package com.lcj.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Map;

@Controller
@RequestMapping("/param")
public class ParamController {
    @RequestMapping("test1")
    public void javaee(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException {
        System.out.println("request:"+request);
        System.out.println("response:"+response);
        System.out.println("session:"+session);
        String imag = request.getServletContext().getRealPath("imag");
        System.out.println("物理路径为："+imag);

        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();
        writer.write("<h2 style='color:green'>hello javaee</h2>");
        writer.flush();
        writer.close();
    }

    @RequestMapping("/test2")
    public void io(InputStream in, OutputStream out)
    {
        System.out.println("inputstream:"+in);
        System.out.println("outputstream:"+out);
    }

    @RequestMapping("/test3")
    public String model(Model model, Map map, ModelMap modelMap)
    {
        model.addAttribute("model","model");
        map.put("map","wang");
        modelMap.put("put","modelmapput");
        modelMap.addAttribute("add","modelmapadd");
        return "result";
    }

    @RequestMapping("/test4")
    public String params(int id,String name,Model model)
    {
        System.out.println("ParamController.params");
        model.addAttribute("result","id="+id+",name="+name);
        return "result";
    }

    @RequestMapping("/test5/{id}/{name}")
    public String path(@PathVariable int id,@PathVariable String name, Model model)
    {
        System.out.println("ParamController.path");
        model.addAttribute("result","id="+id+",name="+name);
        return "result";
    }

    @RequestMapping("/test6")
    public String header(@RequestHeader("User-Agent") String useragent,@RequestHeader("Cookie") String cookie, Model model)
    {
        System.out.println("ParamController.header");
        model.addAttribute("result","useragent="+useragent+",cookie="+cookie);
        return "result";
    }

    @RequestMapping("/addcookie")
    public void addcookie(HttpServletResponse response)
    {
        Cookie cookie = new Cookie("name","james");
        response.addCookie(cookie);
    }

    @RequestMapping("/test7")
    public String cookie(@CookieValue("name") String username,Model model)
    {
        System.out.println("ParamController.cookie");
        model.addAttribute("result","cookie.username"+username);
        return "result";
    }

    @RequestMapping("/test8")
    public String requestbody( String username,String password,@RequestParam(required = false)int id ,Model model)
    {
        System.out.println("ParamController.requestbody");
        model.addAttribute("result","username="+username+",password="+password);
        return "result";
    }
}
